/*
 * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *     
 *     http://www.apache.org/licenses/LICENSE-2.0  
 *     
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.
 */

package com.mcgrobot.plugin.schedule;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.mcgrobot.common.SpringContextHelper;
import com.mcgrobot.common.sysenum.FlowRunModeEnum;
import com.mcgrobot.entity.flow.web.WebStruct;
import com.mcgrobot.service.FlowService;
import com.mcgrobot.service.STimetaskService;


public class FlowScheduleTask {
	private static Logger logger = LoggerFactory.getLogger(FlowScheduleTask.class);
	
	public void run(String data) {
		
		FlowSchedulerTaskParam flowSchedulerTaskParam = JSON.parseObject(data, FlowSchedulerTaskParam.class);
		FlowService flowService = SpringContextHelper.getSpringBean(FlowService.class);
		STimetaskService sTimetaskService = SpringContextHelper.getSpringBean(STimetaskService.class);
		WebStruct webStruct = new WebStruct();
		webStruct.setFlowId(flowSchedulerTaskParam.getFlowId());
		TaskSession taskSession = new TaskSession();
		try {
			STimetask sTimetask = sTimetaskService.selectByPrimaryKey(flowSchedulerTaskParam.getTaskId());
			sTimetask.setStartTime(new Date());
			sTimetaskService.updateByPrimaryKeySelective(sTimetask);
			flowService.generate(webStruct, taskSession, false, null, null, FlowRunModeEnum.SCHEDULER.getValue(), flowSchedulerTaskParam);
			sTimetask.setEndTime(new Date());
			sTimetaskService.updateByPrimaryKeySelective(sTimetask);
		} catch (Exception e) {
			logger.error("流程任务调度执行失败：", e);
		}
	}
}
